package com.xexun.gpstracker.baby.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.xexun.gpstracker.channel.R;

import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */
public class CallAdapter extends BaseAdapter {

    /**
     * 存储所有的Node
     */
    protected List<PhoneNumber> phoneList;
    protected Context mContext;
    protected LayoutInflater mInflater;
    protected ListView phoneListView;

    /**
     * 点击的回调接口
     */
    private OnCallClickListener onCallClickListener;


    public interface OnCallClickListener
    {
        void onClick(PhoneNumber phone,int position);
    }


    public  void setOnCallClickListener(OnCallClickListener OnCallClickListener){
        this.onCallClickListener = OnCallClickListener;

    }

    public CallAdapter(ListView phoneListView,Context context, List<PhoneNumber> list) throws IllegalArgumentException,
            IllegalAccessException
    {
        mContext = context;
        phoneList = list;
        mInflater = LayoutInflater.from(context);
        this.phoneListView= phoneListView;
        phoneListView.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public int getCount() {
        return phoneList.size();
    }

    @Override
    public Object getItem(int position) {
        return phoneList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    //在外面先定义，ViewHolder静态类
    static class ViewHolder
    {

        public TextView content_text;
        public TextView time_text;
        public Button btn;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if(convertView == null)
        {
            convertView = mInflater.inflate(R.layout.warninglist_item, parent, false);
            holder = new ViewHolder();
            holder.content_text = (TextView) convertView
                    .findViewById(R.id.content_text);
            holder.time_text = (TextView) convertView
                    .findViewById(R.id.time_text);
            holder.btn = (Button) convertView.findViewById(R.id.editBtn);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.btn.setTag(position);
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int posi = (int)v.getTag();
                PhoneNumber phone = phoneList.get(posi);
                onCallClickListener.onClick(phone,posi);

            }
        });
        PhoneNumber phoneNumber = phoneList.get(position);

        holder.content_text.setText("\t\t"+phoneNumber.getCallName()+":"+phoneNumber.getCallNumber());

        holder.time_text.setVisibility(View.GONE);
        return convertView;


    }








    private AdapterView.OnItemClickListener onItemClickListener  = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (onCallClickListener != null)
            {
                PhoneNumber phone = phoneList.get(position);
                onCallClickListener.onClick(phone,position);
            }
        }
    };
}
